concrete GrammarChi of Grammar = open Prelude, MorphoChi in {
 
  flags coding=utf8;

  lincat
    Phr  = {s : Str} ;
    S    = {s : Str} ;
    QS   = {s : Str} ;
    Cl   = Clause ;
    QCl  = Clause ;
    NP   = NounPhrase ;
    IP   = NounPhrase ;
    CN   = Noun ;
    Det  = Determiner ;
    IDet = Determiner ;
    AP   = Adjective ;
    Adv  = {s : Str} ;
    AdA  = {s : Str} ;
    VP   = VerbPhrase ;
    N    = Noun ;
    A    = Adjective ;
    V    = Verb ;
    V2   = Verb2 ;
    Conj = Conjunction ;
    Subj = {s : Str} ; 
    Pol  = Bool ; -- {s : Str ; p : Bool} ;

  lin
    PhrS  = postfixSS "。" ;
    PhrQS = postfixSS "？" ;

    --No difference in word order.
    UseCl  pol cl = {s = cl.s ! pol} ;
    UseQCl pol cl = {s = cl.s ! pol} ;
    
    --Question particle added in yes/no questions, not wh-questions    
    QuestCl cl = {s = \\pol => cl.s ! pol ++ questionParticle} ;

    SubjS subj s = {s = subj.s ++ s.s} ;
{-
  Note: SubjS is valid only if the SubjS comes after the main clause.
  No problem in this grammar, but for future, Adverb should be a more 
  complex type; for instance
  
  Subj = {s,s2 : Str} ;
  because_Subj = {s = "因为" ; s2 = "所以" } ;
  SubjS subj s =
    { s = \\ord => case ord of {
         Dir => subj.s ++ s.s ;
         Inv => subj.s ++ s.s ++ subj.s2 } 
    } ;
-}

    PredVP = predVP ;

    QuestVP ip vp = 
      let
        cl = predVP ip vp
      in 
        {s = \\pol => cl.s ! pol ;
         compl = vp.compl } ;

    QuestV2 ip np v2 = {
      s = \\pol => 
          let 
            vp : VerbPhrase = predVerb v2
          in 
          bothWays (ip.s ++ (predVP np vp).s ! pol) v2.c ;
      compl = np.s
    } ;

    ComplV2 v np = insertObject (v.c ++ np.s) (predVerb v) ; --love fish/talk (about++fish)

    ComplAP ap = {  --inherent in adjective whether it takes copula
      s = \\pol => posneg (copula ap.cop) pol ;               
      compl = case ap.cop of {
        True  => ap.s ! De ;
        False => ap.s ! NoRel } ; --no copula, no relation particle (的 "de")
      adv = [] ;
    } ;

    DetCN = detCN ; 

    --Adjective as a modifier marked with 的 "de"
    ModCN ap cn  = {s = ap.s ! De ++ cn.s ; cl = cn.cl} ;

    AdVP adv = insertAdverb adv.s ;

    AdAP ada ap = {
    	s = \\position => ada.s ++ ap.s ! position ;
    	cop = ap.cop
    } ;

    IDetCN = detCN ;

    ConjS c a b = {s = c.s ++ a.s ++ c.s2 ++ b.s} ;
    ConjNP c a b = {s = c.n ++ a.s ++ c.n2 ++ b.s ; n = Pl} ;

    UseN n = n ;
    UseA a = a ;
    UseV = predVerb ;  

    this_Det = {s = "这" ; n = Sg ; cl = True} ; --zhè
    that_Det = {s = "那" ; n = Sg ; cl = True} ; --nà
    these_Det = {s = "这些" ; n = Pl ; cl = False} ; --zhè
    those_Det = {s = "那些" ; n = Pl ; cl = False} ; --nà
    every_Det = {s = "每" ; n = Sg ; cl = True} ; --měi
    theSg_Det = {s = "这" ; n = Sg ; cl = False} ; --Doesn't take classifier
    thePl_Det = {s = "这些" ; n = Pl ; cl = False} ; --same as "these"
    indef_Det = {s = "一" ; n = Sg ; cl = True} ; 
    plur_Det = {s = [] ; n = Pl ; cl = False} ;
    two_Det = {s = "二" ; n = Pl ; cl = True} ;

    today_Adv = { s = "今天" } ; 

    very_AdA = {s = "很"} ; --hěn
    which_IDet = {s = "哪" ; n = Sg ; cl = True} ; --nǎ --Does this take classifier?


    --Different conjunctions for nouns, verbs, adjectives and sentences
    and_Conj = {
    	s = "又" ; s2 = "又" ; --yòu, yòu
        n = "及" ; n2 = "又"   --jí,  yòu
    } ;

    because_Subj = {s = "因为"} ; 

    PPos = True ;
    PNeg = False ;

  oper

-- TYPES
    NounPhrase = {s : Str ; n : Number} ;
    VerbPhrase = {s : Bool => Str ; 
                  compl : Str ; adv : Str } ;
    Clause = {s : Bool => Str } ;


-- CLAUSES
{-The structure of the VP is unnecessarily complex for this grammar.
  First plan was to start a full Chinese rg, so we left room for 
  different word orders and information structures.
-}

    predVP : NounPhrase -> VerbPhrase -> Clause = \np,vp ->
    { s = \\pol => 
       let 
         v = vp.s ! pol ;
         np = np.s ;
         compl = vp.compl ;
         adv = vp.adv         
       in
        np ++ v ++ compl ++ adv } ;


    predVerb : Verb -> VerbPhrase = \verb -> {
      s = \\pol => posneg verb.s pol ;
      compl = [] ;
      adv   = [] ;

      } ;

    insertObject : Str -> VerbPhrase -> VerbPhrase = \obj,vp -> {
      s = vp.s ;
      compl = obj ;
      adv = vp.adv 
    } ;

    insertAdverb : Str -> VerbPhrase -> VerbPhrase = \adv,vp -> {
      s = vp.s ;
      compl = vp.compl ;
      adv = adv
    } ;

    posneg : Str -> Bool -> Str = \verb,pol -> case pol of {
       True  => verb ;
       False => negationParticle ++ verb
    } ;

    --stative adjectives and nouns need a copula "shì"
    --verb-like adjectives are without copula
    copula : Bool -> Str = \stative -> case stative of {
      True  => "是" ;
      False => []
    } ;

--MODIFIERS


    detCN : Determiner -> Noun -> NounPhrase = \det,cn ->
      let        
        dcl : Str = getDet det cn
      in
        {s = dcl ++ cn.s ; n = det.n} ;

    --Det : determiner word + classifier (inherent in noun).
    --Plural determiners and some singular don't take classifier.
    --Also no classifier if empty determiner.
    getDet : Determiner -> Noun -> Str = \det,cn -> 
      case det.cl of {
      	True  => det.s ++ cn.cl ;
      	False => det.s
    } ;
    
}